package com.simpleframe.validator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.simpleframe.validator.validators.EmailValidator;
import com.simpleframe.web.action.SimpleframeAction;

public class ValidatorContext {
	
	
	private List<Validator> validators = new ArrayList<Validator>();
	
	public static ValidatorContext getContext(){
		return new ValidatorContext();
	}
	
	public ValidatorContext add(Validator validator){
		validators.add(validator);
		return this;
	}
	
	public void valida(SimpleframeAction action){
		try {
			if(validators!=null){
				for (Validator validator : validators) {
					//设置页面提交过来的值，这个不应放在这里 - 构造时应放进去
					//如果继承了ActionMessage
					//执行验证
					System.out.println("开始验证\n"+validator.getMessage());
					validator.validate(action);
				}
			}
		} finally {
			//e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args) throws ClassNotFoundException {
		FieldValidator validator = new EmailValidator("");
		validator.setFieldName("mail");
		validator.validate("yjafd@@123.com");
		
		
	}
	
	/*
	 * ValidatorContext vc = new VaildatorContext();
	 * vc.add(new EmailValidator(xxx,"邮件"));
	 * vc.add(new MaxlengthValidator(aaa,20,"描述"));
	 * vc.if(new EqualValidator(isXXX,1,))
	 * 		.add(RequiredValidator(ttttt,"分数")); 
	 */
	
	/*
	 * ValidatorContext vc = new VaildatorContext();
	 * vc.addMail(xxx,"邮件");
	 * vc.addMaxlength(aaa,20,"描述");
	 * vc.ifEqual(isXXX,1)
	 * 	.addRequired(tttt,"分数")
	 * 		.ifMax(tttt,30)
	 * 			.addMax(xxxx,40,"第二重分");
	 */
}
